#include "knowledgebasemainwindow.h"
#include "ui_knowledgebasemainwindow.h"
#include "edit_html.h"
#include <QtDebug>

KnowledgebaseMainWindow::KnowledgebaseMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::KnowledgebaseMainWindow)
{
    setObjectName("KnowledgebaseMainWindow");

    ui->setupUi(this);

    htmlEditor = new Edit_html();
    htmlEditor->set_Cache(IMM_BUILD);

    // 隐藏菜单
    //this->menuBar()->hide();

    //QMenu *menu = this->menuBar()->addMenu(QObject::tr("&File"));
    //menu->addAction(QObject::tr("Open File"),htmlEditor, SLOT(OpenNewDoc()));
    //menu->addSeparator();
    //menu->addAction(QObject::tr("&Quit"),this, SLOT(quit()));

    //QMenu *menu0 = this->menuBar()->addMenu(QObject::tr("&About"));
    //menu0->addAction(QObject::tr("About Programm %1").arg(_PROGRAM_NAME_),this, SLOT(Aboutp()));
    //menu0->addAction(QObject::tr("Autor Home Page"),this, SLOT(Link0()));
    //menu0->addAction(QObject::tr("Check update..."),this, SLOT(Link1()));

    connect(htmlEditor, SIGNAL(statusMessage(QString)),this->statusBar(), SLOT(showMessage(QString)));

    this->ui->tabWidgetContent->clear();
    this->ui->tabWidgetContent->insertTab(0,htmlEditor
                                       ,QIcon(":/knowledgebase/images/applications-office-2.png")
                                       ,"Welcome");
    int lastTab = this->ui->tabWidgetContent->insertTab(this->ui->tabWidgetContent->count(),new QWidget()
                                       ,QIcon(":/knowledgebase/images/appointment-new-4.png")
                                       ,"");
    // create a new article when clicked
    connect(this->ui->tabWidgetContent,SIGNAL(lastTabClicked)
            ,this,SLOT(newArticle()));
    this->ui->tabWidgetContent->setCurrentIndex(lastTab);

    //this->setCentralWidget(htmlEditor);
}

KnowledgebaseMainWindow::~KnowledgebaseMainWindow()
{
    delete ui;
}

void KnowledgebaseMainWindow::showArticlesListByDir(QString dir)
{
    // display all the article in dir
    qDebug() << "Show articles in :" << dir;

    fileModel = new QFileSystemModel();
    fileModel->setRootPath(dir);
    QStringList fileEx;
    fileEx << "*.jpeg" << "*.html";

    fileModel->setNameFilters(QStringList() << "*.jpeg" << "*.jpg" << "*.png");
    fileModel->setFilter(QDir::Files);

    this->ui->listViewArticles->setModel(fileModel);
    this->ui->listViewArticles->setRootIndex(fileModel->index(dir));
}

void KnowledgebaseMainWindow::on_listViewArticles_clicked(QModelIndex index)
{
    QString filePath = this->fileModel->filePath(index);
    QFileInfo fi(filePath);
    QString ext = fi.suffix();
    ext = ext.toLower();
    if (ext == "xhtml" || ext == "html" || ext == "xml" || ext == "xcms") {
        htmlEditor->SetFileBase(filePath);
    } else {
        QMessageBox::warning(0,"Error \"XHTML Editor\" ",QString("The file \"%1\" is not xhtml or html").arg(fi.fileName()));
    }
}

void KnowledgebaseMainWindow::writeSettings()
{
    QSettings settings("QLuoshu","Knowledgebase");
    //settings.setValue("root",);
}

void KnowledgebaseMainWindow::newArticle()
{
    qDebug() << this->objectName() << ":new article";
    int tabCount = this->ui->tabWidgetContent->count();
    int newTabIndex = this->ui->tabWidgetContent->insertTab(tabCount - 1
                                          ,htmlEditor
                                          ,QIcon(":/knowledgebase/images/applications-office-2.png")
                                          ,tr("New Article")
                                          );
    this->ui->tabWidgetContent->setCurrentIndex(newTabIndex);
}
